package com.woniuxy.company5.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.company5.dto.UserCompanyDto;
import com.woniuxy.company5.entity.UserCompany;
import com.woniuxy.company5.service.UserCompanyService;
import com.woniuxy.company5.util.Result;
import com.woniuxy.company5.vo.UserCompanyVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/userCompany")
public class UserCompanyController {
    @Resource
    private UserCompanyService userCompanyService;
    @PostMapping("query")
    public Result queryByLike(@RequestBody UserCompanyDto userCompanyDto){
        PageHelper.startPage(userCompanyDto.getCurrentPage(),userCompanyDto.getPageSize(),true);
        List<UserCompanyVo> userCompany = userCompanyService.selectByLike(userCompanyDto);
        PageInfo<UserCompanyVo> pageInfo = new PageInfo<>(userCompany);
        return Result.ok().data("userList",userCompany).data("total",pageInfo.getTotal());
    }
    @PostMapping("")
    public Result add(@RequestBody UserCompany userCompany){
        userCompanyService.insertSelective(userCompany);
        return Result.ok().data("uid",userCompany.getId());
    }
    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Long id){
        UserCompany userCompany = userCompanyService.selectByPrimaryKey(id);
        return Result.ok().data("user",userCompany);
    }
    @PutMapping("")
    public Result update(@RequestBody UserCompany userCompany){
        userCompanyService.updateByPrimaryKeySelective(userCompany);
        return Result.ok();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        userCompanyService.deleteByPrimaryKey(id);
        return Result.ok();
    }
}
